$().ready(function() {

});
function save_checklist_user(){
	var intCheckListID = $("#intCheckListID").val();
	var intCheckListUserID = $("#intCheckListUserID").val(); 
	var strDescription = $("#checklist_strDescription").val();
	session_code = $.cookie('session_code');
	xfunc = "checklist/save_checklist_user";
	data_sent = {
			'intID':intCheckListUserID,
			'intCheckListID':intCheckListID,
			'session_code':session_code,
			'strDescription':strDescription,
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request		
		    $( ".result-message-box" ).dialog({
		    	title: "Save Check List",
		    	autoOpen: false,
		    	minHeight: 140,
				modal: true,
				draggable: false ,
				show: {
					effect: "explode",
					duration: 300
				},
				hide: {
					effect: "explode",
					duration: 300
				},	      
				buttons: {
					"Ok": function() {
						$(this).dialog("close");
					}
				}
		      });
		    $('.ui-dialog-content').text(result.message);
			$( ".result-message-box" ).dialog("open");			    
		}
	});	
	ret_checklist();
}
function show_checklist_edit(intID){
	$("#intCheckListUserID").val(intID);
	if(intID!=""){
		xfunc = "checklist/get_checklist_user";
		data_sent = {
				'intID':intID
		};
		
		$.ajax({ 
			type: "POST", // request type POST
			url: base_url+xfunc, // post to PHP file
			data: data_sent, // request send to PHP file
			dataType:'json',
			success: function(result){ // result = return from php request		
				$("#checklist_strDescription").val(result.strDescription);
			}
		});	
	}
	$(".check-list-user-box").fadeOut("fast", function(){					
		$(".check-list-user-editor-box").fadeIn("fast", function(){
			$('.result-message-box').html("");
		});	
	});	
}

function checklist(){
	html='';
	html+='<div class="wrapper-check-list">';
		html+='<div class="check-list"></div>';
		html+='<i class="clearfix"></i>';
		html+='<div class="check-list-user-box checklist-hide hide">';
			html+='<div class="check-list-user"></div>';
			html+='<a class="ret-checklist" title="Cancel"></a>';
			html+='<a class="new-checklist" title="New"></a>';
		html+='</div>';
		html+='<i class="clearfix"></i>';
		html+='<div class="check-list-user-editor-box checklist-hide hide">';
			html+='<div class="check-list-description-editor">Description:&nbsp;<input type="text" id="checklist_strDescription" /></div>';
			html+='<a class="ret-checklist-editor" title="Cancel"></a>';
			html+='<a class="save-checklist-editor" title="Save"></a>';
		html+='</div>';
		html+='<i class="clearfix"></i>';
		html+='</div>';	
	html+='<div class="result-message-box">&nbsp;</div>';	
	$('.box-two-content').html(html);
	$('.box-two .title span').text("Check List");
	clear_checklist();
	list_checklist();
	switchwindow("checklist");
	$('.ret-checklist').click(function(){
		ret_checklist();
	});
	$('.new-checklist').click(function(){
		$(".check-list-user-box").fadeOut("fast", function(){					
			$(".check-list-user-editor-box").fadeIn("fast", function(){
				$('.result-message-box').html("");
			});	
		});
	});
	$('.ret-checklist-editor').click(function(){
		ret_checklist();
	});
	$('.save-checklist-editor').click(function(){
		save_checklist_user();
	});	
}
function checklist_nav(){
	var chk_log = $.cookie('chk_log');	
	if(chk_log==1){	
		html='';
		html+='<div class="wrapper-check-list">';
			html+='<div class="check-list"></div>';
			html+='<i class="clearfix"></i>';
			html+='<div class="check-list-user-box checklist-hide hide">';
				html+='<div class="check-list-user"></div>';
				html+='<a class="ret-checklist" title="Cancel"></a>';
				html+='<a class="new-checklist" title="New"></a>';
			html+='</div>';
			html+='<i class="clearfix"></i>';
			html+='<div class="check-list-user-editor-box checklist-hide hide">';
				html+='<div class="check-list-description-editor">Description:&nbsp;<input type="text" id="checklist_strDescription" /></div>';
				html+='<a class="ret-checklist-editor" title="Cancel"></a>';
				html+='<a class="save-checklist-editor" title="Save"></a>';
			html+='</div>';
			html+='<i class="clearfix"></i>';
			html+='</div>';	
		html+='<div class="result-message-box">&nbsp;</div>';	
		$('.box-two-content').html(html);
		$('.box-two .title span').text("Check List");		
		clear_checklist();
		list_checklist();	
		switchwindow_no_animate("checklist");
		$('.ret-checklist').click(function(){
			ret_checklist();
		});
		$('.new-checklist').click(function(){
			$(".check-list-user-box").fadeOut("fast", function(){					
				$(".check-list-user-editor-box").fadeIn("fast", function(){
					$('.result-message-box').html("");
				});	
			});
		});
		$('.ret-checklist-editor').click(function(){
			ret_checklist();
		});
		$('.save-checklist-editor').click(function(){
			save_checklist_user();
		});		
	}
}
function clear_checklist(){
	$('.result-message-box').html("");
	$("#checklist_strDescription").val("");
	$("#intCheckListID").val("");
	$("#intCheckListUserID").val("");
}
function ret_checklist(){
	clear_checklist();
	list_checklist();
	$('#check-list-box>.title>span').text("Check list");
	$(".checklist-hide").each(function(k,v){
		var block = $(this).css("display");
		if(block=="block"){
			$(this).fadeOut("fast", function(){					
				$(".check-list").fadeIn("fast", function(){
					
				});	
			});
		}
	});

}
function list_checklist(){
	var html = "";
	xfunc = "checklist/list_checklist";
	data_sent = {};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request
			
			$(result.intID).each(function(k,v){
				html+="<a onclick='list_checklist_user(\""+result.intID[k]+"\",\""+result.strTitle[k]+"\");'>";
				img_temp = result.strImgLink[k];
					if(img_temp==""){
						img_temp = "test.png";
					}
					html+='<div class="img-checklist-wrapper">';
						html+='<img src="'+base_url+'assets/uploaded/img/thumbnail/'+result.strImgLink[k]+'">';
					html+='</div>';
					html+='&nbsp;'+result.strTitle[k];
				html+="</a>";
				
			});
			$(".check-list").html(html);
		}
	});	
}
function list_checklist_user(intID,subtitle){
	$("#intCheckListID").val(intID);
	session_code = $.cookie('session_code');
	html='';
	xfunc = "checklist/list_checklist_user";
	data_sent = {
			'session_code':session_code,
			'intCheckListID':intID
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request		
			var title = $('#check-list-box>.title>span').text(subtitle);
			html = '<ul>';
			$(result.intID).each(function(k,v){
				chk = "";
				divchk = "checklist-img-uncheck";
				if(result.intStatus[k]==1){
					chk = "checked";
					divchk = "checklist-img-check";
				}
				html+='<li>';
					html+='<ul>';
						html+='<li>';
							html+='<div class="check-list-img-div-wrapper" id="check-list-img-div-wrapper_'+result.intID[k]+'" onclick="update_status(this.id);">';
								html+='<div id="div_checklist_'+result.intID[k]+'" class="check-list-img-div '+divchk+'">';
								html+='</div>';
							html+='</div>';
							html+='<input type="checkbox" class="checklist-checkbox hide" id="chk_checklist_'+result.intID[k]+'" onclick="update_status(this.id);" '+chk+' />';
						html+='</li>';
						html+='<li>';
							html+='<a onclick="show_checklist_edit('+result.intID[k]+');"><i class="check-list-description">'+result.strDescription[k]+'</i></a>';
						html+='</li>';						
					html+='</ul>';
				html+='</li>';
			});
			html+= '</ul>';
			$('.check-list-user').html(html);
			$('.check-list').fadeOut("fast",function(){
				$('.check-list-user-box').fadeIn("fast");
			});
		}
	});		
}
function update_status(id){
	var get_id = id.split("_");
	var intStatus = 0;
	var obj_check = $("#chk_checklist_"+get_id[get_id.length-1]);
	var checked = obj_check.prop("checked");
	session_code = $.cookie('session_code');
	if(get_id[0]=="check-list-img-div-wrapper"){		
		if(checked==true){
			obj_check.prop("checked",false);
		} else {
			obj_check.prop("checked",true);
		}
	}	
	checked = obj_check.prop("checked");
	
	
	if(checked==true){
		intStatus = 1;
	}
	var div = $("#div_checklist_"+get_id[get_id.length-1])
	
	xfunc = "checklist/update_intstatus_checklist";
	data_sent = {
			'intStatus':intStatus,
			'session_code':session_code,
			'intID':get_id[get_id.length-1]
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request		
			//$('.result-message-box').html(result.message);
			var checked_div = $("#chk_checklist_"+get_id[get_id.length-1]).prop("checked");
			div.removeClass(div.attr("class"));	
			$("#div_checklist_"+get_id[get_id.length-1]).addClass("check-list-img-div");			
			if(checked_div==true){
				$("#div_checklist_"+get_id[get_id.length-1]).addClass("checklist-img-check");
			} else {
				$("#div_checklist_"+get_id[get_id.length-1]).addClass("checklist-img-uncheck");
			}
		}
	});	
}